package br.edu.fateczl.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import br.edu.fateczl.model.Reserva;

public class ReservaDAO {
	 
Connection c;
	
	public ReservaDAO(){
		GenericDAO gDao = new GenericDAO();
		c = gDao.getConnection();
	}
	public boolean insereReserva(Reserva reserva){
		boolean inserido = false;
		String sql = "INSERT INTO reserva (codigo,data_reserva,reservado,ordem_fila) VALUES (?,?,?,?)";
		
		try{
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, reserva.getCodigo());
			ps.setDate(2, (Date) reserva.getDataReserva());
			ps.setBoolean(3,reserva.getReservado());
			ps.setInt(4, reserva.getOrdemFila());
			ps.execute();
			ps.close();
			inserido = true;
		}catch(SQLException e){
			JOptionPane.showMessageDialog(null,e.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
		}
		return inserido;
	}
	public boolean atualizaReserva(Reserva reserva){
		boolean atualizado= false;
		String sql = "UPDATE reserva Set codigo,data_reserva,reservado,ordem_fila WHERE codigo = ?";
		try {
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, reserva.getCodigo());
			ps.setDate(2, (Date) reserva.getDataReserva());
			ps.setBoolean(3,reserva.getReservado());
			ps.setInt(4, reserva.getOrdemFila());
			ps.execute();
			ps.close();
			atualizado = true;
		}catch(SQLException e){
			JOptionPane.showMessageDialog(null,e.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
		}
		return atualizado;
		
	}
	public boolean excluiReserva(Reserva reserva){
		boolean excluido = false;
		String sql = "DELETE reserva WHERE codigo = ?";
		try {
			PreparedStatement ps = c.prepareStatement(sql);
			ps.setInt(1, reserva.getCodigo());
			ps.execute();
			ps.close();
			excluido = true;
		}catch(SQLException e){
			JOptionPane.showMessageDialog(null,e.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
		}
		return excluido;
	}
	public Reserva consultaReserva(Reserva reserva){
		Reserva reservaConsultada = new Reserva();
		String sql = "SELECT codigo,data_reserva,reservado,ordem_fila FROM reserva Where codigo = ?";
		try {
			PreparedStatement ps = c.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				reservaConsultada.setCodigo(rs.getInt("codigo"));
				reservaConsultada.setDataReserva(rs.getDate("data_reserva"));
				reservaConsultada.setReservado(rs.getBoolean("reservado"));
				reservaConsultada.setOrdemFila(rs.getInt("ordem_fila"));
			}
			rs.close();
			ps.close();
		}catch(SQLException e){
			JOptionPane.showMessageDialog(null,e.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
		}
		return reservaConsultada;
	}
	
	public List<Reserva> consultaListaReserva(){
		List<Reserva> listaReserva = new ArrayList<Reserva>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT codigo,data_reserva,reservado,ordem_fila");
		sql.append("FROM reserva");
		try {
			PreparedStatement ps = c.prepareStatement(sql.toString());
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Reserva reservaConsultada = new Reserva();
				reservaConsultada.setCodigo(rs.getInt("codigo"));
				reservaConsultada.setDataReserva(rs.getDate("data_reserva"));
				reservaConsultada.setReservado(rs.getBoolean("reservado"));
				reservaConsultada.setOrdemFila(rs.getInt("ordem_fila"));
				listaReserva.add(reservaConsultada);
			}
			rs.close();
			ps.close();
		}catch(SQLException e){
			JOptionPane.showMessageDialog(null,e.getMessage(),"Erro",JOptionPane.ERROR_MESSAGE);
		}
		return listaReserva;
	}

}
